package com.ruoyi.web.controller.news;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.system.domain.news.NewsPolicyLaw;
import com.ruoyi.system.domain.news.dto.NewsPolicyLawDto;
import com.ruoyi.system.domain.news.query.NewsPolicyLawQuery;
import com.ruoyi.system.domain.news.vo.NewsPolicyLawVo;
import com.ruoyi.system.service.news.NewsPolicyLawService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/news/newsPolicyLaw")
@Api(value = "newsPolicyLawController", tags  = "政策标准")
public class NewsPolicyLawController extends BaseController {

	@Resource
	private NewsPolicyLawService newsPolicyLawService;

    @ApiOperation(value = "新增/修改政策标准信息", notes = "新增/修改政策标准信息")
	@PostMapping("/submit")
	public R submit(@RequestBody NewsPolicyLawDto newsPolicyLawDto) {
		LoginUser loginUser = SecurityUtils.getLoginUser();
		NewsPolicyLaw newsPolicyLaw = new NewsPolicyLaw();
		BeanUtils.copyProperties(newsPolicyLawDto,newsPolicyLaw);
		if (newsPolicyLaw.getId()==null) {
			newsPolicyLaw.setCreateBy(loginUser.getUsername());
			newsPolicyLaw.setDeptId(loginUser.getDeptId());
			newsPolicyLaw.setCreateTime(new Date());
			newsPolicyLaw.setUpdateBy(loginUser.getUsername());
			newsPolicyLaw.setUpdateTime(new Date());
		} else {
			newsPolicyLaw.setUpdateBy(loginUser.getUsername());
			newsPolicyLaw.setUpdateTime(new Date());
		}
		boolean result = newsPolicyLawService.saveOrUpdate(newsPolicyLaw);
		if (result) {
			return R.ok();
		} else {
			return R.fail("操作失败");
		}
	}

	@ApiOperation(value = "删除政策标准信息", notes = "删除政策标准信息")
	@DeleteMapping("/delete")
	public R delete(@RequestParam(value = "id") Long id) {
		boolean result = newsPolicyLawService.removeById(id);
		if (result) {
			return R.ok();
		} else {
			return R.fail("操作失败");
		}
	}

	@ApiOperation(value = "获取单个政策标准信息", notes = "获取单个政策标准信息")
	@GetMapping("/getById")
	public R<NewsPolicyLawVo> getById(@RequestParam(value = "id") Long id) {
		NewsPolicyLawQuery newsPolicyLawQuery = new NewsPolicyLawQuery();
		newsPolicyLawQuery.setId(id);
		List<NewsPolicyLawVo> newsPolicyLawList = newsPolicyLawService.getNewsPolicyLawList(newsPolicyLawQuery);
		if (newsPolicyLawList!=null&&newsPolicyLawList.size()>0) {
			return R.ok(newsPolicyLawList.get(0));
		} else {
			return R.fail("数据不存在");
		}
	}

	@ApiOperation(value = "获取政策标准信息列表")
	@PostMapping(value = "/list")
	public R<PageInfo<NewsPolicyLawVo>> list(@RequestBody NewsPolicyLawQuery newsPolicyLawQuery) {
		List<NewsPolicyLawVo> newsPolicyLawList = newsPolicyLawService.getNewsPolicyLawList(newsPolicyLawQuery);
		return R.ok(new PageInfo<>(newsPolicyLawList));
	}
}
